package com.qujia.station.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.exception.BaseException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.station.domain.CustomListColumn;
import com.qujia.station.domain.dto.CustomListColumnDto;
import com.qujia.station.domain.vo.CustomListColumnVo;
import com.qujia.station.service.CustomListColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Api(tags = "订单自定义字段")
@RestController
@RequestMapping("/customListColumn")
@Slf4j
public class CustomListColumnController {

    @Autowired
    private CustomListColumnService customListColumnService;

    @PostMapping("/addListColumn")
    @ApiOperation("新增或修改自定义字段")
    public R addListColumn(@RequestBody CustomListColumnDto customListColumnDto) {
        return R.ok(customListColumnService.addListColumn(customListColumnDto));
    }

    @GetMapping("/selectListColumn")
    @ApiOperation("查询自定义字段")
    public R<CustomListColumnVo> selectListColumn(@RequestParam Integer tableId) {
        return R.ok(customListColumnService.selectListColumn(tableId));
    }

    @PostMapping("/get")
    @ApiOperation(value = "查询日报表列配置",httpMethod = "POST")
    public AjaxResult getCustomListColumn(@RequestBody CustomListColumn customListColumn){
        if(Objects.isNull(customListColumn) || Objects.isNull(customListColumn.getTableId())){
            return AjaxResult.error("请求参数异常");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(Objects.isNull(user) || Objects.isNull(user.getUserId())){
            return AjaxResult.error("用户不存在,请重新登录");
        }

        customListColumn.setUserId(user.getUserId());
        log.info("查询日报表列配置getCustomListColumn 请求参数:[{}]", JSONObject.toJSONString(customListColumn));
        CustomListColumn custom = customListColumnService.selectCustomListColumnByUserId(customListColumn);
        log.info("查询日报表列配置getCustomListColumn 返回结果:[{}]", JSONObject.toJSONString(custom));
        return AjaxResult.success(custom);
    }


    @PostMapping("/save")
    @ApiOperation(value = "插入日报表列配置",httpMethod = "POST")
    public AjaxResult saveCustomListColumn(@RequestBody CustomListColumn customListColumn){
        if(Objects.isNull(customListColumn) || StringUtils.isEmpty(customListColumn.getColumnId())
                || Objects.isNull(customListColumn.getTableId())){
            throw new BaseException("请求参数异常");
        }
        log.info("插入日报表列配置saveCustomListColumn 请求参数:[{}]", JSONObject.toJSONString(customListColumn));
        CustomListColumn custom = customListColumnService.saveCustomListColumn(customListColumn);
        log.info("插入日报表列配置saveCustomListColumn 返回结果:[{}]", JSONObject.toJSONString(custom));
        return AjaxResult.success(custom);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改日报表列配置",httpMethod = "POST")
    public AjaxResult updateCustomListColumn(@RequestBody CustomListColumn customListColumn){
        if(Objects.isNull(customListColumn) || Objects.isNull(customListColumn.getId())){
            throw new BaseException("请求参数异常");
        }
        log.info("修改日报表列配置updateCustomListColumn 请求参数:[{}]", JSONObject.toJSONString(customListColumn));
        int i = customListColumnService.updateCustomListColumn(customListColumn);
        log.info("修改日报表列配置updateCustomListColumn 返回结果:[{}]", i);
        if(i <= 0){
            throw new BaseException("修改失败");
        }
        return AjaxResult.success(i);

    }
}
